package com.menu.Service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.menu.Mapper.BackgroundMapper;
import com.menu.Pojo.Background;
import com.menu.Pojo.Menu;
import com.menu.Service.BackgroundService;
import com.menu.Utils.FileUtils;
import com.menu.Utils.ResponseResult;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BackgroundServiceImpl implements BackgroundService {
    @Resource
    BackgroundMapper backgroundMapper;
    @Resource
    FileUtils fileUtils;

    @Override
    public ResponseResult findBackgroundByState() {
        List<Background> background=backgroundMapper.findBackGroundByStateOne();
        if(background.isEmpty()){
            return ResponseResult.success("没有背景，启动默认背景","https://cp1.douguo.com/upload/caiku/c/7/1/690x390_c771e79cf28e205344fa426383a7c311.jpg");
        }
        return ResponseResult.success("切换背景成功",background);
    }

    @Override
    public ResponseResult addBackground(MultipartFile backgroundSrc) {
        Background background1=new Background();
        background1.setState(0);
        backgroundMapper.addBackground(background1);
        ResponseResult responseResult=fileUtils.uploadBackGround(backgroundSrc,background1.getBackgroundId());
        if(responseResult.getCode()==200){
            Object object=responseResult.getObject();
            String fileName=(String)object;
            backgroundMapper.updateBackGround(background1.getBackgroundId(),fileName);
        } else{
            return responseResult;
        }
        Background background= backgroundMapper.findBackgroundById(background1.getBackgroundId());
        return ResponseResult.success("添加成功",background);
    }

    @Override
    public ResponseResult updateBackGround(Integer backgroundId,MultipartFile backgroundSrc) {
        Background background= backgroundMapper.findBackgroundById(backgroundId);
        if(background==null){
            return ResponseResult.error("不存在该背景");
        }
        ResponseResult responseResult=fileUtils.uploadBackGround(backgroundSrc,backgroundId);
        if(responseResult.getCode()==200){
            Object object=responseResult.getObject();
            String fileName=(String)object;
            backgroundMapper.updateBackGround(backgroundId,fileName);
        } else{
            return responseResult;
        }
        return ResponseResult.success("修改成功");
    }

    @Override
    public ResponseResult findAll(Integer pageNow, Integer pageSize) {
        PageHelper.startPage(pageNow,pageSize);
        List<Background> backgroundList= backgroundMapper.findAll();
        PageInfo<Background> pageInfo=new PageInfo<>(backgroundList);
        return ResponseResult.success("查询成功",pageInfo);
    }

    @Override
    public ResponseResult deleteBackground(Integer backgroundId) {
        Background background= backgroundMapper.findBackgroundById(backgroundId);
        if(background==null){
            return ResponseResult.error("不存在该背景");
        }
        backgroundMapper.deleteBackground(backgroundId);
        return ResponseResult.success("删除成功");
    }

    @Override
    public ResponseResult batchDeleteBackground(Integer[] backgroundId) {
        backgroundMapper.batchDeleteBackground(backgroundId);
        return ResponseResult.success("删除成功");
    }

    @Override
    public ResponseResult updateBackgroundToOne(Integer backgroundId) {
        Background background= backgroundMapper.findBackgroundById(backgroundId);
        if(background==null){
            return ResponseResult.error("不存在该背景");
        }
        List<Background> backgrounds=backgroundMapper.findBackGroundByStateOne();
        List<Integer> c= backgrounds.stream().map(Background::getBackgroundId).collect(Collectors.toList());
        for(Integer x:c){
            backgroundMapper.updateStateToZero(x);
        }
        backgroundMapper.updateStateToOne(backgroundId);
        return ResponseResult.success("设置背景图片成功");
    }
}
